<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Car extends Model
{

    use Common, SoftDeletes;

    protected $table = 'car';

    protected $hidden = ['updated_at', 'deleted_at'];

    protected $casts = ['license_driving' => 'array'];
    protected $fillable = [
        'number','type','brand_id','company_id'
    ];
    /**
     * @param $value
     * @return array
     * @Author 姿势就是力量
     */
    public function getRestrictionAttribute($value)
    {
        return explode(',', $value);
    }


    /**
     * 车辆品牌
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @Author 姿势就是力量
     */
    public function brand()
    {
        return $this->belongsTo(CarBrand::class, 'brand_id', 'id');
    }


    /**
     * 车辆所属管理公司
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @Author 姿势就是力量
     */
    public function company()
    {
        return $this->belongsTo(Company::class, 'company_id', 'id');
    }


    /**
     * 车辆所属司机
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     * @Author 姿势就是力量
     */
    public function driver()
    {
        return $this->hasManyThrough(
            Drivers::class,
            DriverCar::class,
            'car_id',
            'id',
            'id',
            'driver_id'
        );
    }


    /**
     * 车辆正面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @Author 姿势就是力量
     */
    public function front()
    {
        return $this->hasOne(UploadFile::class, 'id', 'front')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 车辆侧面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @Author 姿势就是力量
     */
    public function side()
    {
        return $this->hasOne(UploadFile::class, 'id', 'side')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 车辆背面
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @Author 姿势就是力量
     */
    public function back()
    {
        return $this->hasOne(UploadFile::class, 'id', 'back')->select(['id', 'file_url', 'file_name']);
    }


    /**
     * 业务类型
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @Author 姿势就是力量
     */
    public function service()
    {
        return $this->belongsTo(ServiceType::class, 'type', 'id');
    }
}
